package tests;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import junit.framework.Assert;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;


public class TestHibernate {
	@Test
	public void testThatHibernameWorks() {
		String contextFile="classpath:bankapp-test-persistence-beans.xml";
		ClassPathResource cpres = new ClassPathResource(contextFile);
		
		Assert.assertNotNull(cpres);
		Assert.assertTrue(cpres.isReadable());
		
		ClassPathXmlApplicationContext repositoryContext = new ClassPathXmlApplicationContext(contextFile);
		
		EntityManagerFactory emf = repositoryContext.getBean("entityManagerFactory", EntityManagerFactory.class);
		
		LocalContainerEntityManagerFactoryBean ggg;
		EntityManager em = emf.createEntityManager();
		
		
		User user = new User();
		user.setName("avatar");
		//user.setId(0);
		user.setAddress(new Address());
		em.getTransaction().begin();
		em.persist(user);

		em.getTransaction().commit();
		
		User saved = em.find(User.class, (long)1);
		Assert.assertNotNull(saved);
		Assert.assertEquals("avatar", saved.getName());
		
		em.close();
		emf.close();
	}
	
	
}
// 